import java.io.*;
import java.util.*;
import Exceptions.BigFileException;
public class Anagram {
    Output output=new Output();
    Input input=new Input();
    public List getWordsForSearch() throws IOException,FileNotFoundException{
        String currentLine;
        String currentLineChanged;
        Set<String> setFileForCheck=new TreeSet<String>();
        Map<String,String> mapFileForCheck=new HashMap<String,String>();
        System.out.println("Write path to file which contains text, please...");
        File file=new File(input.getPathForRead());
        FileInputStream FIS=new FileInputStream(file);
        InputStreamReader ISR=new InputStreamReader(FIS,"Cp1251");
        BufferedReader BR=new BufferedReader(ISR);
        while((currentLine=BR.readLine())!=null)
        {
            currentLineChanged=currentLine.toLowerCase().replaceAll("[,.-]","");
            String [] currentLineWords=currentLineChanged.split("\\s");
            for(int i=0;i<currentLineWords.length;i++)
            {
                String wordInLine=currentLineWords[i].toString();
                setFileForCheck.add(wordInLine);
            }
            for(String getWord:setFileForCheck)
            {
              char[]charWordInLine=getWord.toCharArray();
              Arrays.sort(charWordInLine);
                String stringWordInLine2=new String(charWordInLine);    //key
                mapFileForCheck.put(stringWordInLine2, getWord);
            }
        }
        BR.close();
        ISR.close();
        FIS.close();
        List<String> listWordsForSearch=new ArrayList<String>(mapFileForCheck.values());
        return listWordsForSearch;
    }
    public List getWordsFromFile() throws IOException, BigFileException {
        String currentLine;
        String currentLineChanged;
        List <String> wordsFromFile = new ArrayList<String> ();
        File file=new File(input.getPathForRead());
        if (100>=file.length()/(1024*3)|1<=file.length())
        {
            FileInputStream FIS=new FileInputStream(file);
            InputStreamReader ISR=new InputStreamReader(FIS,"Cp1251");
            BufferedReader BR=new BufferedReader(ISR);
            while((currentLine=BR.readLine())!=null){
             currentLineChanged=currentLine.toLowerCase().replaceAll("[,.-]","");
                String [] currentLineWords=currentLineChanged.split("\\s");
                for(int j=0;j<currentLineWords.length;j++)
                {
                    String wordInLine=currentLineWords[j].toString();
                    wordsFromFile.add(wordInLine);}
            }
            BR.close();
            ISR.close();
            FIS.close();
        }else{
            throw new BigFileException();
        }
        return wordsFromFile;
    }

  public void search() throws IOException,BigFileException {
      Set <String> result = new HashSet<String> ();
      List <String>listWordsFromFile= (List) getWordsFromFile();
      List<String>listWordsForSearch=(List) getWordsForSearch();
      for (String wordForSearch: listWordsForSearch)
      {
          char[]charWordForSearch=wordForSearch.toCharArray();
          for( int i=0;i<listWordsFromFile.size();i++){
              char[]charWordInLine=(listWordsFromFile.get(i).toCharArray());
              Arrays.sort(charWordInLine);
              Arrays.sort(charWordForSearch);
              String stringWordInLine=new String(charWordInLine);
              String stringWordForSearch=new String(charWordForSearch);
              if(stringWordForSearch.equals(stringWordInLine))
              {
                  if(listWordsFromFile.get(i).equals(wordForSearch)){
                  }else{
                      result.add(wordForSearch + " ");
                      result.add(listWordsFromFile.get(i));
                  }
              }
          }
       output.write(result);result.clear();
      }
  }
}
